package com.zhiyou100.crm.servlet.message;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhiyou100.crm.pojo.AdminBaseServlet;
import com.zhiyou100.crm.pojo.Message;
import com.zhiyou100.crm.pojo.User;
import com.zhiyou100.crm.service.MessageService;
import com.zhiyou100.crm.service.UserService;
import com.zhiyou100.crm.service.impl.MessageServiceImpl;
import com.zhiyou100.crm.service.impl.UserServiceImpl;

/**
 * 更新邮件
 */
@WebServlet("/message/update")
public class UpdateServlet extends AdminBaseServlet {
	private static final long serialVersionUID = 1L;
	MessageService messageService=new MessageServiceImpl();
	UserService userService=new UserServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		int messageId = Integer.parseInt(request.getParameter("id"));
		List<Message> messages = messageService.queryMessage();
		List<User> users = userService.queryAllUser();
		if(messageId>0)
		for (int i = 0; i < messages.size(); i++) {
			if(messageId==(messages.get(i).getMessage_id())){
				request.setAttribute("message",messages.get(i));
				request.setAttribute("users",users);
				break;
			}
		}
		request.getRequestDispatcher("/WEB-INF/view/message/update.jsp").forward(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		User user = (User) request.getSession().getAttribute("user");
		int receiver = Integer.parseInt(request.getParameter("receiver"));
		String subject = request.getParameter("subject");
		String content = request.getParameter("content");
		int message_id = Integer.parseInt(request.getParameter("messageId"));
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		long l = System.currentTimeMillis();
		String format = df.format(new Date(l));
		Timestamp time = Timestamp.valueOf(format);
		// 注意这里判断用户点哪个按扭的方法！
		boolean send = request.getParameter("send") != null;
		boolean save = request.getParameter("save") != null;
		if (send) {
			//发送
			//receiver接收者
			Message message = new Message(message_id,subject,content,time,1);
			int a = messageService.update(message);
			if (a > 0) {
				response.sendRedirect(request.getContextPath() + "/message/listSend");
			}
		} else if (save) {
			//保存
			Message message = new Message(message_id,subject,content,time,0);
			int b = messageService.update(message);
			if (b > 0) {
				response.sendRedirect(request.getContextPath() + "/message/listSave");
			}
		}


	}

}
